#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib,urllib2
import re
from HTMLParser import HTMLParser
from mutagen.mp3 import MP3
from mutagen.id3 import TIT2,TPE1,TALB,TDRC,TCON
from os.path import isfile

"""
TODO:
	-XXX récupérer nom de fichier plus sympa
	- edit mp3 tag
	-XXX télécharger la liste des fichiers
	- vérifier fichier avant de dl
"""


#####################################################################

def url_to_mp3(url):
	page = urllib2.urlopen(url)
	regexp = re.compile(r"sites%2Fdefault.+\.mp3")
	for line in page:
	    match = regexp.search(line)
	    if match:
	        mp3 = match.group(0)
	        break
	mp3_url = "http://www.franceinter.fr/" + urllib2.unquote(mp3)
	return mp3_url

def download_file(url,file_name):
	# file_name = url.split('/')[-1]
	u = urllib2.urlopen(url)
	f = open(file_name, 'wb')
	meta = u.info()
	file_size = int(meta.getheaders("Content-Length")[0])
	print u"\tTéléchargement de %s (taille : %s)" % (file_name, sizeof_fmt(float(file_size)))

	file_size_dl = 0
	block_sz = 8192
	while True:
	    buffer = u.read(block_sz)
	    if not buffer:
	        break
	    file_size_dl += len(buffer)
	    f.write(buffer)
	    status = "\t%3.1fMo  [%2d%%]" % (file_size_dl/(1024.**2), int(file_size_dl*100./file_size))
	    status = status + chr(8)*(len(status)+1)
	    print status,
	f.close()

def sizeof_fmt(num):
    for x in ['octets','Ko','Mo','Go']:
        if num < 1024.0:
            return "%3.1f%s" % (num, x)
        num /= 1024.0
    return "%3.1f%s" % (num, 'To')

#####################################################################

class MyHTMLParser(HTMLParser):

	def __init__(self):
		self.state = 0
		self.data = []

		self.current_title = ""
		self.current_date = ()
		self.current_mp3 = ""

		self.reid = re.compile(r'play=(?P<id>\d+)')
		self.redate = re.compile(r'du (?P<jour>\d{2})\/(?P<mois>\d{2})\/(?P<annee>\d{4})')
		self.base = "http://www.franceinter.fr/player/reecouter?play="
		HTMLParser.__init__(self)

	def handle_starttag(self, tag, attrs):
		# print "Start tag:", tag
		if self.state == 0 and tag == 'div':
			for attr in attrs:
				if attr[0] == 'class' and attr[1] == 'content':
					self.state = 1
					print "============================================"
					# print "loulou"

		if self.state == 1 and tag == 'h3':
			self.state = 2

		if self.state == 1 and tag == 'div':
			for attr in attrs:
				if attr[0] == 'class' and 'date' in attr[1]:
					self.state = 3

		if self.state == 1 and tag == 'div':
			for attr in attrs:
				if attr[0] == 'class' and 'ecouter' in attr[1]:
					self.state = 4

		if self.state == 4 and tag == 'a':
			for attr in attrs:
				if attr[0] == 'href' and self.reid.search(attr[1]):
					match = self.reid.search(attr[1])
					link = self.base + match.group('id')
					print link
					mp3link = url_to_mp3(link)
					print mp3link
					self.current_mp3 = mp3link
					# print "\n"
					self.state = 0
					self.add_data()

	def add_data(self):
		self.data.append((self.current_title,self.current_date,self.current_mp3))
		self.current_title = ""
		self.current_date = ()
		self.current_mp3 = ""

		# print "     attr:", attr

	# def handle_endtag(self, tag):
	# 	# pass
	# 	if tag == 'html':
	# 		print "GG"
	# 		return self.data
		# print "End tag  :", tag

	def handle_data(self, data):
		if self.state == 2:
			title = data
			self.current_title = title
			print data
			self.state = 1

		if self.state == 3:
			match = self.redate.search(data)
			jj,mm,aa = match.group('jour'),match.group('mois'),match.group('annee')
			print aa+"-"+mm+"-"+jj
			self.current_date = (jj,mm,aa)
			self.state = 1
		# pass
		# print "Data     :", data

	# def close(self):
	# 	HTMLParser.close()
	# 	return self.data

    # def handle_comment(self, data):
    #     print "Comment  :", data
    # def handle_entityref(self, name):
    #     c = unichr(name2codepoint[name])
    #     print "Named ent:", c
    # def handle_charref(self, name):
    #     if name.startswith('x'):
    #         c = unichr(int(name[1:], 16))
    #     else:
    #         c = unichr(int(name))
    #     print "Num ent  :", c
    # def handle_decl(self, data):
    #     print "Decl     :", data


#####################################################################



emission = "http://www.franceinter.fr/archives-diffusions/137151/"

mois_list = [
	# "2011-02",
	# "2011-03",
	# "2011-04",
	# "2011-05",
	# "2011-06",
	# "2011-07",
	# "2011-08",
	# "2011-09",
	# "2011-10",
	# "2011-11",
	# "2011-12",
	# "2012-01",
	# "2012-02",
	# "2012-03",
	# "2012-04",
	# "2012-05",
	# "2012-06",
	# "2012-07",
	"2012-08",
	
]

download_folder = u"/media/Data/Sur les épaules de Darwin/"

#####################################################################


data = []
# files_url = []

for mois in mois_list:
	print "\nMois :",mois
	page_url = emission + mois
	# print page_url

	parser = MyHTMLParser()

	page = urllib2.urlopen(page_url)
	encoding = page.headers.getparam('charset')
	pagecontent = page.read().decode(encoding)

	parser.feed(pagecontent)
	data.extend(parser.data)
	# print parser.data


	
	# page = urllib.urlopen(page_url)
	# base = "http://www.franceinter.fr/player/reecouter?play="
	# # regexp = re.compile(r'play=(?P<id>\d+)')
	# regexp = re.compile(r'du (?P<jour>\d{2})\/(?P<mois>\d{2})\/(?P<annee>\d{4}).+play=(?P<id>\d+)',re.DOTALL)
	# # regexp = re.compile(r"^http://www\.franceinter\.fr/player/reecouter\?play=.+")

	# # html = page.read()
	# # match = regexp.search(page.read())
	# # if match:
	# # 	print match.group()


	# for line in page:
	#     match = regexp.search(line)
	#     if match:
	#     	# print line
	#     	print match.group('jour'),match.group('mois'),match.group('annee')
	#         link = base + match.group('id')
	#         # print line
	#         print link
	#         files_url.append(url_to_mp3(link))

    
# print len(files_url)

print ""
print u"Nombre d'émissions trouvées :",len(data)

for title,date,url in data:
	filename = date[2]+"-"+date[1]+"-"+date[0] + " - " + title + ".mp3"

	if isfile(download_folder+filename):
		print u"Le fichier "+filename+u" existe déjà..."
	else:
		download_file(url,download_folder+filename)
	
	audio = MP3(download_folder+filename)
	audio["TIT2"] = TIT2(encoding=3, text=[title])
	audio["TPE1"] = TPE1(encoding=3, text=u"Jean-Claude Ameisen")
	audio["TALB"] = TALB(encoding=3, text=u"Sur les épaules de Darwin")
	audio["TDRC"] = TDRC(encoding=3, text=date[2])
	# audio.add(TCON(encoding=3, text=u"Genr"))
	audio.save()
	# print "  - Téléchargement de",filename
	# urllib.urlretrieve(url,download_folder+filename)